package com.rp.data.tcp_ws.server_tcp.echo;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * Created by jht on 17-7-3.
 */
public class ByteUtils {

    public static String codeToChinese(byte[] bytes) throws UnsupportedEncodingException {
        String Chinese = "";
        for (int i = 0; i < bytes.length; i += 2) {
            byte[] tmpBytes = new byte[2];
            tmpBytes[0] = (byte) ((bytes[i] & 0xFF)+160);
            tmpBytes[1] = (byte) ((bytes[i+1] & 0xFF)+160);
            String chara = new String(tmpBytes, "GB2312");
            Chinese += chara;
            System.out.println(chara);
        }
        return Chinese;
    }


    public static String byteToBinaryString(byte b) {
        StringBuilder builder = new StringBuilder();
        int a = b;
        for (int n = 0; n < 8; n++) {
            byte byte1 = (byte) a;
            int temp = (a >> 1) << 1;
            if (temp == byte1)
                builder.append("0");
            else
                builder.append("1");
            a = temp >> 1;
        }
        return builder.reverse().toString();
    }

    public short littleEndianToShort(byte[] bytes) {
        return ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getShort();
    }

    public int littleEndianToInt(byte[] bytes) {
        return ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getInt();
    }

    public short bigEndianToShort(byte[] bytes) {
        return ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN).getShort();
    }

    public int bigEndianToInt(byte[] bytes) {
        return ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN).getInt();
    }

    public static byte[] shortToBytes(short n) {
        byte[] b = new byte[2];
        b[0] = (byte) ((n >> 8) & 0xff);
        b[1] = (byte) (n & 0xff);
        return b;
    }

    public static short byteToShort(byte[] b) {
        short s = 0;
        short s0 = (short) (b[0] & 0xff);// 最低位
        short s1 = (short) (b[1] & 0xff);
        s1 <<= 8;
        s = (short) (s0 | s1);
        return s;
    }

    public static short bytesToShort(byte[] b) {
        return (short) (b[1] & 0xff | (b[0] & 0xff) << 8);
    }

    public static byte[] intToByte(int a) {
        return new byte[]{
                (byte) ((a >> 24) & 0xFF),
                (byte) ((a >> 16) & 0xFF),
                (byte) ((a >> 8) & 0xFF),
                (byte) (a & 0xFF)
        };
    }

    public static int byteToInt(byte[] b) {
        return b[3] & 0xFF |
                (b[2] & 0xFF) << 8 |
                (b[1] & 0xFF) << 16 |
                (b[0] & 0xFF) << 24;
    }

    public static String byteToHex(byte b) {
        StringBuffer buffer = new StringBuffer();
        String temp = Integer.toHexString(b & 0xff);
        if (temp.length() < 2) {
            buffer.append("0");
        }
        buffer.append(temp);
        return buffer.toString().toUpperCase();
    }

    public static String bytesToHex(byte[] bytes) {
        return bytesToHex(bytes, bytes.length);
    }

    public static String bytesToHex(byte[] bytes, int len) {
        if (bytes == null) {
            return "null";
        }
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < len; i++) {
            String temp = Integer.toHexString(bytes[i] & 0xff);
            if (temp.length() < 2) {
                buffer.append("0");
            }
            buffer.append(temp + " ");
        }
        return buffer.toString().toUpperCase();
    }

}
